
//_______________________________________________________________________________________________________________
/****************************************************************************************************************/
/*                                                                                                              */
/*                     P O I N T E R S   T O   M E S S A G E S                                                  */
/*                                                                                                              */
/****************************************************************************************************************/
// These macro used in services 
#if defined(OS_ENABLE_INT_MSG)
    #define __OS_MSG_DI()       {_OS_temp = OS_DI();}
    #define __OS_MSG_RI()       {OS_RI(_OS_temp);}
#else
    #define __OS_MSG_DI()
    #define __OS_MSG_RI()
#endif


//------------------------------------------------------------------------------
// Check for message exists

#define OS_CheckMsg(pmsg)          ((pmsg).status & 1)
#define OS_CheckMsg2(pmsg)          ((pmsg).status)

//------------------------------------------------------------------------------
// Create message and zero it.

#define OS_CreateMsg(pmsg)         {__OS_MSG_DI(); (pmsg).status = 0; (pmsg).msg = (OST_MSG)0;__OS_MSG_RI();}
#define OS_ClearMsg(pmsg)          {(pmsg).status = 0;}


//------------------------------------------------------------------------------
// Send message. If message is already exists then wait when it became free.

#define OS_SendMsg(pmsg, value)                                             \
    {                                                                       \
        if (OS_CheckMsg(pmsg)) _OS_WAIT_EVENT(!OS_CheckMsg(pmsg));          \
        __OS_MSG_DI();                                                      \
        (pmsg).status = 1;                                                  \
        (pmsg).msg = (OST_MSG) value;                                       \
        __OS_MSG_RI();                                                      \
    }

//------------------------------------------------------------------------------
// Send message. If message is already exists then it will be overwritten.

#define OS_SendMsgNoWait(pmsg, value)                                       \
    {                                                                       \
        OS_Flags.bEventError = 0;                                           \
        if (OS_CheckMsg(pmsg)) OS_Flags.bEventError = 1;                    \
        __OS_MSG_DI();                                                      \
        (pmsg).status = 1;                                                  \
        (pmsg).msg = (OST_MSG) value;                                       \
        __OS_MSG_RI();                                                      \
    }

//------------------------------------------------------------------------------
// Send message. If message is already exists then wait when it became free. Exit if timeout expired.
#define OS_SendMsgTimeout(pmsg, value,timeout)                              \
    {                                                                       \
        OS_Flags.bTimeout = 0;                                              \
        if (OS_CheckMsg(pmsg))                                              \
        {                                                                   \
            _OS_WAIT_EVENT_TIMEOUT(!OS_CheckMsg(pmsg), timeout);            \
        }                                                                   \
        if (!OS_IsTimeout()){                                               \
            __OS_MSG_DI();                                                  \
            (pmsg).status = 1;                                              \
            (pmsg).msg = (OST_MSG) value;                                   \
            __OS_MSG_RI();                                                  \
        }                                                                   \
    }

//------------------------------------------------------------------------------
// Wait for message. After waiting message is set free

#define OS_WaitMsg(pmsg, os_msg_type_var)                           \
    {                                                               \
        _OS_WAIT_EVENT(OS_CheckMsg(pmsg));                          \
        __OS_MSG_DI();                                              \
        os_msg_type_var = (pmsg).msg;                               \
        OS_ClearMsg(pmsg);                                          \
        __OS_MSG_RI();                                              \
    }


//------------------------------------------------------------------------------
// Wait for mesage. Exit if timeout expired.
#define OS_WaitMsgTimeout(pmsg, os_msg_type_var,timeout)            \
    {                                                               \
        os_msg_type_var = (OST_MSG) 0;                              \
        _OS_WAIT_EVENT_TIMEOUT(OS_CheckMsg(pmsg), timeout);         \
        if (!OS_IsTimeout()) {                                      \
            __OS_MSG_DI();                                          \
            os_msg_type_var = (pmsg).msg;                           \
            OS_ClearMsg(pmsg);                                      \
            __OS_MSG_RI();                                          \
        }                                                           \
    }


//------------------------------------------------------------------------------
// Accept existing message. After accepting message is set free.
#define OS_AcceptMsg(pmsg, os_msg_type_var)                         \
    {                                                               \
        __OS_MSG_DI();                                              \
        os_msg_type_var = pmsg.msg;                                 \
        OS_ClearMsg(pmsg);                                          \
        __OS_MSG_RI();                                              \
    }


//------------------------------------------------------------------------------
#ifdef OS_ENABLE_INT_MSG
// Send message from interrupt. If message is already exists then it will be overwritten.
#define OS_IntSendMsgNoWait(pmsg, value)                                    \
    {                                                                       \
        OS_Flags.bEventError = 0;                                           \
        if (OS_CheckMsg(pmsg)) OS_Flags.bEventError = 1;                    \
        pmsg.status = 1;                                                    \
        pmsg.msg = (OST_MSG) value;                                         \
    }


#endif  // OS_ENABLE_INT_SERVICE

